/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package babydisco.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author Stefan
 */
public class DataReader {

//    test code:
//    public static void main(String[] args) throws IOException{
//        read("d:\\samples1.txt");
//    }

    // de data wordt in een array van arraylists gestopt, aangezien er 6 colummen zijn in de data
    // (time, thorax1, thorax2, abdomen1, abdomen2, abdomen3, annotations) en die colummen kunnen variabel in lente zijn.
    public static ArrayList[] read(String fileName) throws IOException {
//        test code:
//        StringBuilder text = new StringBuilder();
//        String NL = System.getProperty("line.separator");
        Scanner scanner = new Scanner(new FileInputStream(fileName));
        //skip de eerste 2 regels
        scanner.nextLine();
        scanner.nextLine();
        int i = 0;
        ArrayList[] data = new ArrayList[7];
        for(int j = 0; j<data.length;j++){
            data[j] = new ArrayList();
        }
        try {
            while (scanner.hasNext()){
                if(i == 0){
                    data[i].add(Double.parseDouble(scanner.next()));  //tijd
                    i++;
                } else if (i < 3){
                    data[i].add(Double.parseDouble(scanner.next()) * Math.pow(10, -3));   //thorax
                    i++;
                } else if(i<6){
                    data[i].add(Double.parseDouble(scanner.next()) * Math.pow(10, -6));   //abdomen
                    i++;
                } else{
                    data[i].add(Double.parseDouble(scanner.next()));    //annotations
                    i = 0;
                }
            }
        }
        finally{
            scanner.close();
        }

//        test code:
//        for(int l = 0; l<data[0].size();l++){
//            for(int k = 0; k<7; k++){
//                text.append(data[k].get(l) + " ");
//            }
//            text.append(NL);
//        }
//        System.out.println(text.toString());

        return data;
    }
    
    public static ArrayList[] readFake(String fileName) throws IOException {
        Scanner scanner = new Scanner(new FileInputStream(fileName));
        
        int i = 0;
        ArrayList[] data = new ArrayList[7];
        for(int j = 0; j<data.length;j++){
            data[j] = new ArrayList();
        }
        try {
            while (scanner.hasNext()){
                if(i < 6){
                    data[i].add(Double.parseDouble(scanner.next()));
                    i++;
                } else{
                    data[i].add(Double.parseDouble(scanner.next()));
                    i = 0;
                }
            }
        }
        finally{
            scanner.close();
        }

        return data;
    }
    
    /**
     * Special reader for trainingData (generated by matlab)
     * @param fileName full filename
     * @return and arraylist with 0 - input, 1 - ideal output
     * @throws IOException 
     */
    public static ArrayList[] readTrainingData(String fileName) throws IOException 
    {
        Scanner scanner = new Scanner(new FileInputStream(fileName));
        
        int i = 0;
        ArrayList[] data = new ArrayList[2];
        for(int j = 0; j<data.length;j++){
            data[j] = new ArrayList();
        }
        try {
            while (scanner.hasNext()){
                if(i == 0){
                    scanner.next(); //time
                    data[i].add(Double.parseDouble(scanner.next()));  //input (noisy)
                    i++;
                    data[i].add(Double.parseDouble(scanner.next()));  //ideal (baby)
                    scanner.next(); //mother
                    scanner.next(); //0 line
                    scanner.next(); //0 line
                    scanner.next(); //0 line
                    i = 0;
                }
            }
        }
        finally{
            scanner.close();
        }

        return data;
    }
}
